package aos.ydksxt.service.xxdjgl;

import aos.framework.core.typewrap.Dto;
import aos.framework.core.typewrap.Dtos;
import aos.framework.core.utils.AOSJson;
import aos.framework.core.utils.AOSUtils;
import aos.framework.web.router.HttpModel;
import aos.ydksxt.dao.DjTdsyqdjxx;
import aos.ydksxt.dao.DjTdsyqdjxxJmxx;
import aos.ydksxt.service.common.CommonService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 土地使用权登记管理服务
 * Created by Stiofan on 2016/12/23.
 */
@Service
public class TdsyqdjglService extends CommonService {
    @Override
    @Transactional
    public void initView(HttpModel httpModel) {
        httpModel.setViewPath("/ydksxt/xxdjgl/tdsyqdjgl.jsp");
    }

    @Transactional
    public void queryTdsyqdjxxByTdsyquuid(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto();
        param.put("tdsyquuid", inDto.get("tdsyquuid"));
        DjTdsyqdjxx djTdsyqdjxx = (DjTdsyqdjxx) persistenceDAO.findSingleByHql("from DjTdsyqdjxx xx where xx.tdsyquuid=:tdsyquuid and xx.yxbz='Y'", param);
        Dto outDto = Dtos.newOutDto();
        outDto.setAppCode("suc");
        outDto.putAll((Map) AOSUtils.object2Map(djTdsyqdjxx));
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
    @Transactional
    public void queryTdsyqdjxxBydjxh(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto("djxh", inDto.get("djxh"));
        List results = persistenceDAO.findAllBySql("select syq.*,\n" +
                "       xx.tdzdh,\n" +
                "       xx.xzqhds_dm,\n" +
                "       xx.xzqhxq_dm,\n" +
                "       xx.jdxz_dm,\n" +
                "       xx.zldz_sqjq_dm,\n" +
                "       xx.zldz_dlxd_dm,\n" +
                "       xx.zldz_lh,\n" +
                "       xx.tdzyz,\n" +
                "       syq.tdsyq_ysje - syq.tdsyq_msje tdsyq_yjse\n" +
                "  from ydks.dj_tdsyqdjxx syq, ydks.dj_tdjcxx xx\n" +
                " where syq.tdbh = xx.tdbh\n" +
                "   and xx.yxbz = 'Y'\n" +
                "   and syq.yxbz = 'Y'\n" +
                "   and syq.tdsyqlx_dm != '03'\n" +
                "   and syq.djxh = :djxh\n", param);
        for (Object object : results) {
            Map<String, Object> map = (Map<String, Object>) object;
            String xzqhdsDm = (String) map.get("xzqhdsDm");
            String xzqhxqDm = (String) map.get("xzqhxqDm");
            String jdxzDm = (String) map.get("jdxzDm");
            String sqjqDm = (String) map.get("zldzSqjqDm");
            String dlxdDm = (String) map.get("zldzdlxdDm");
            String dzLh = (String) map.get("zldzLh");
            map.put("zldz", getFullAddress(xzqhdsDm, xzqhxqDm, jdxzDm, sqjqDm, dlxdDm, dzLh));
        }
        httpModel.setOutMsg(AOSJson.toJson(AOSUtils.object2Map(results)));
    }
    @Transactional
    public void saveTdsyqdjxx(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto param = Dtos.newDto("tdsyquuid", inDto.get("tdsyquuid"));
        DjTdsyqdjxx djTdsyqdjxx = (DjTdsyqdjxx) persistenceDAO.findSingleByHql("from DjTdsyqdjxx xx where xx.tdsyquuid=:tdsyquuid", param);
        if (djTdsyqdjxx == null) {
            djTdsyqdjxx = new DjTdsyqdjxx();
        }
        AOSUtils.copyProperties(httpModel.getInDto(), djTdsyqdjxx);
        djTdsyqdjxx.setYxbz("Y");
        if (StringUtils.isEmpty(djTdsyqdjxx.getTdsyquuid())) {
            djTdsyqdjxx.setTdsyquuid(getRandomUUID());
            djTdsyqdjxx.setLrrDm(getCzryDm(httpModel));
            djTdsyqdjxx.setSjgsdq(getCzjgDm(httpModel));
            djTdsyqdjxx.setLrrq(getDateTime());
        } else {
            djTdsyqdjxx.setXgrDm(getCzryDm(httpModel));
            djTdsyqdjxx.setXgrq(getDateTime());
        }
        Dto outDto = Dtos.newOutDto();
        if (djTdsyqdjxx.getTdsyqQsrq() != null && djTdsyqdjxx.getTdsyqZzrq() != null) {
            if (djTdsyqdjxx.getTdsyqQsrq().after(djTdsyqdjxx.getTdsyqZzrq())) {
                outDto.setAppCode("err");
                outDto.setAppMsg("使用权终止日期小于等于使用权起始日期,请检查相关数据是否准确!");
            }
        } else {
            Dto checkParams = Dtos.newDto();//
            checkParams.put("tdbh", inDto.get("tdbh"));
            List<DjTdsyqdjxx> tdsyqs = persistenceDAO.findAllByHql("from DjTdsyqdjxx xx where xx.tdbh=:tdbh and xx.yxbz='Y'", checkParams);
            BigDecimal tdysymj = djTdsyqdjxx.getTdsyqYsmj().add(djTdsyqdjxx.getTdsyqMsmj());
            for (DjTdsyqdjxx tdsyq : tdsyqs) {
                if (!tdsyq.getTdsyquuid().equals(djTdsyqdjxx.getTdsyquuid())) {//累计已经使用的土地面积，如果累计面积大于土地面积，则不允许保存
                    tdysymj.add(tdsyq.getTdsyqYsmj().add(tdsyq.getTdsyqMsmj()));
                    if (StringUtils.compare(String.valueOf(djTdsyqdjxx.getDjxh()), String.valueOf(tdsyq.getDjxh())) == 0 &&
                            StringUtils.compare(djTdsyqdjxx.getTdsyqlxDm(), tdsyq.getTdsyqlxDm()) == 0 &&
                            StringUtils.compare(djTdsyqdjxx.getTdsyrlxDm(), tdsyq.getTdsyrlxDm()) == 0) {
                        outDto.setAppCode("err");
                        outDto.setAppMsg("同一纳税人、使用人类型、使用权类型不允许存在多条重复数据，请直接在原基础上进行修改!");
                        break;
                    }
                    //校验同一人是否存在土地使用权交叉情况，如果存在则不允许保存
                    if (StringUtils.compare(tdsyq.getTdsyqlxDm(), djTdsyqdjxx.getTdsyqlxDm()) == 0 &&
                            StringUtils.compare(tdsyq.getTdsyrlxDm(), djTdsyqdjxx.getTdsyrlxDm()) == 0) {
                        if (djTdsyqdjxx.getTdsyqQsrq().before(tdsyq.getTdsyqQsrq()) && djTdsyqdjxx.getTdsyqZzrq().after(tdsyq.getTdsyqQsrq()) &&
                                (djTdsyqdjxx.getTdsyqQsrq().before(tdsyq.getTdsyqZzrq()) && djTdsyqdjxx.getTdsyqZzrq().after(tdsyq.getTdsyqZzrq()) &&
                                        (djTdsyqdjxx.getTdsyqQsrq().after(tdsyq.getTdsyqQsrq()) && djTdsyqdjxx.getTdsyqZzrq().before(tdsyq.getTdsyqZzrq()) &&
                                                (djTdsyqdjxx.getTdsyqQsrq().before(tdsyq.getTdsyqQsrq()) && djTdsyqdjxx.getTdsyqZzrq().after(tdsyq.getTdsyqZzrq()))))) {
                            outDto.setAppCode("err");
                            outDto.setAppMsg("同一使用人类型及使用权类型，不允许存在多条重复数据!");
                            break;
                        }
                    }
                }
            }
            if (tdysymj.compareTo(inDto.getBigDecimal("tdzmj")) > 0) {
                outDto.setAppCode("err");
                outDto.setAppMsg("本次登记的使用权面积与已登记的使用权面积之和(" + tdysymj + ")大于土地总面积(" + inDto.getBigDecimal("tdzmj") + "),请检查所填应税面积、免税面积是否正确!");
            }
            if (!"err".equals(outDto.getAppCode())) {
                try {
                    persistenceDAO.saveOrUpdate(djTdsyqdjxx);
                    outDto.setAppCode("suc");
                } catch (Exception e) {
                    outDto.setAppMsg("err");
                    outDto.setAppMsg("使用权信息保存时发生异常，请检查相关信息是否准确!");
                    logger.error(e);
                }
            }
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
    @Transactional
    public void zfTdsysdjxxByTdsyquuid(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto params = Dtos.newDto("tdsyquuid", inDto.get("tdsyquuid"));
        DjTdsyqdjxx djTdsyqdjxx = (DjTdsyqdjxx) persistenceDAO.findSingleByHql("from DjTdsyqdjxx xx where xx.tdsyquuid=:tdsyquuid", params);
        djTdsyqdjxx.setZfrq(getDateTime());
        djTdsyqdjxx.setZfrDm(getCzryDm(httpModel));
        djTdsyqdjxx.setZfyy(inDto.getString("zfyy"));
        djTdsyqdjxx.setYxbz("N");
        djTdsyqdjxx.setXgrDm(getCzryDm(httpModel));
        djTdsyqdjxx.setXgrq(getDateTime());
        Dto outDto = Dtos.newOutDto();
        try {
            persistenceDAO.saveOrUpdate(djTdsyqdjxx);
            outDto.setAppCode("suc");
        } catch (Exception e) {
            outDto.setAppCode("err");
            outDto.setAppMsg(e.getMessage());
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
    @Transactional
    public void queryTdsyqJmxxByTdsyquuid(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto params = Dtos.newDto("tdsyquuid", inDto.get("tdsyquuid"));
        List jmxxs = persistenceDAO.findAllBySql("select dj.tddjmxuuid,\n" +
                "       dj.tdbh,\n" +
                "       dj.tddj_dm,\n" +
                "       dj.dwse_dm,\n" +
                "       dj.sebz,\n" +
                "       dj.tdmj,\n" +
                "       dj.ynse,\n" +
                "       jm.tdsyqjmuuid,\n" +
                "       jm.tdjmxz_dm,\n" +
                "       jm.jmmj,\n" +
                "       jm.jmse,\n" +
                "       dj.ynse-jm.jmse yjse,\n" +
                "       jm.jm_yxqq,\n" +
                "       jm.jm_yxqz\n" +
                "  from ydks.dj_tdjcxx_tddjmx dj\n" +
                "  left join ydks.dj_tdsyqdjxx xx\n" +
                "    on dj.tdbh = xx.tdbh\n" +
                "  left join ydks.dj_tdsyqdjxx_jmxx jm\n" +
                "    on dj.tddjmxuuid = jm.tddjmxuuid\n" +
                "   and xx.tdsyquuid = jm.tdsyquuid\n" +
                "   and jm.yxbz = 'Y'\n" +
                " where xx.tdsyquuid = :tdsyquuid\n" +
                "   and dj.yxbz = 'Y'\n" +
                "   and xx.yxbz = 'Y'\n", params);
        httpModel.setOutMsg(AOSJson.toGridJson(jmxxs));
    }
    @Transactional
    public void saveTddjJmmxxx(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto params = Dtos.newDto();
        params.put("tdsyqjmuuid", inDto.get("tdsyqjmuuid"));
        DjTdsyqdjxxJmxx djTdsyqdjxxJmxx = (DjTdsyqdjxxJmxx) persistenceDAO.findSingleByHql("from DjTdsyqdjxxJmxx mx where mx.tdsyqjmuuid=:tdsyqjmuuid", params);
        if (djTdsyqdjxxJmxx == null) {
            djTdsyqdjxxJmxx = new DjTdsyqdjxxJmxx();
        }
        AOSUtils.copyProperties(inDto, djTdsyqdjxxJmxx);
        djTdsyqdjxxJmxx.setYxbz("Y");
        if (StringUtils.isEmpty(djTdsyqdjxxJmxx.getTdsyqjmuuid())) {
            djTdsyqdjxxJmxx.setTdsyqjmuuid(getRandomUUID());
            djTdsyqdjxxJmxx.setLrrDm(getCzryDm(httpModel));
            djTdsyqdjxxJmxx.setLrrq(getDateTime());
        } else {
            djTdsyqdjxxJmxx.setXgrDm(getCzryDm(httpModel));
            djTdsyqdjxxJmxx.setXgrq(getDateTime());
            if ("N".equals(djTdsyqdjxxJmxx.getYxbz())) {
                djTdsyqdjxxJmxx.setZfyy(inDto.getString("zfyy"));
                djTdsyqdjxxJmxx.setZfrq(getDateTime());
                djTdsyqdjxxJmxx.setZfrDm(getCzryDm(httpModel));
            }
        }
        Dto outDto = Dtos.newOutDto();
        try {
            persistenceDAO.saveOrUpdate(djTdsyqdjxxJmxx);
            outDto.putAll((Map) AOSUtils.object2Map(djTdsyqdjxxJmxx));
            params.clear();
            params.put("tdsyquuid", djTdsyqdjxxJmxx.getTdsyquuid());
            persistenceDAO.updateBySql("update ydks.dj_tdsyqdjxx xx\n" +
                    "   set xx.tdsyq_msmj =\n" +
                    "       (select sum(jm.jmmj)\n" +
                    "          from ydks.dj_tdsyqdjxx_jmxx jm\n" +
                    "         where jm.tdsyquuid = xx.tdsyquuid\n" +
                    "           and jm.yxbz = 'Y'),\n" +
                    "       xx.tdsyq_msje =\n" +
                    "       (select sum(jm.jmse)\n" +
                    "          from ydks.dj_tdsyqdjxx_jmxx jm\n" +
                    "         where jm.tdsyquuid = xx.tdsyquuid\n" +
                    "           and jm.yxbz = 'Y')\n" +
                    " where xx.tdsyquuid = :tdsyquuid", params);
            outDto.setAppCode("suc");
        } catch (Exception e) {
            outDto.setAppCode("err");
            outDto.setAppMsg(e.getMessage());
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
    @Transactional
    public void zfTddjjmxxByTdsyqjmuuid(HttpModel httpModel) {
        Dto inDto = httpModel.getInDto();
        Dto params = Dtos.newDto();
        params.put("tdsyqjmuuid", inDto.get("tdsyqjmuuid"));
        DjTdsyqdjxxJmxx djTdsyqdjxxJmxx = (DjTdsyqdjxxJmxx) persistenceDAO.findSingleByHql("from DjTdsyqdjxxJmxx mx where mx.tdsyqjmuuid=:tdsyqjmuuid", params);
        djTdsyqdjxxJmxx.setYxbz("N");
        djTdsyqdjxxJmxx.setXgrDm(getCzryDm(httpModel));
        djTdsyqdjxxJmxx.setXgrq(getDateTime());
        djTdsyqdjxxJmxx.setZfyy(inDto.getString("zfyyJm"));
        djTdsyqdjxxJmxx.setZfrq(getDateTime());
        djTdsyqdjxxJmxx.setZfrDm(getCzryDm(httpModel));
        Dto outDto = Dtos.newOutDto();
        try {
            persistenceDAO.saveOrUpdate(djTdsyqdjxxJmxx);
            outDto.putAll((Map) AOSUtils.object2Map(djTdsyqdjxxJmxx));
            params.clear();
            params.put("tdsyquuid", djTdsyqdjxxJmxx.getTdsyquuid());
            persistenceDAO.updateBySql("update ydks.dj_tdsyqdjxx xx\n" +
                    "   set xx.tdsyq_msmj =\n" +
                    "       (select sum(jm.jmmj)\n" +
                    "          from ydks.dj_tdsyqdjxx_jmxx jm\n" +
                    "         where jm.tdsyquuid = xx.tdsyquuid\n" +
                    "           and jm.yxbz = 'Y'),\n" +
                    "       xx.tdsyq_msje =\n" +
                    "       (select sum(jm.jmse)\n" +
                    "          from ydks.dj_tdsyqdjxx_jmxx jm\n" +
                    "         where jm.tdsyquuid = xx.tdsyquuid\n" +
                    "           and jm.yxbz = 'Y')\n" +
                    " where xx.tdsyquuid = :tdsyquuid", params);
            outDto.setAppCode("suc");
        } catch (Exception e) {
            outDto.setAppCode("err");
            outDto.setAppMsg(e.getMessage());
        }
        httpModel.setOutMsg(AOSJson.toJson(outDto));
    }
}
